layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#shopList',
        url : 'http://127.0.0.1:88/index.php/shop/shop/shopList?id='+localStorage.login_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "shopListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'shop_name', title: '店铺名', minWidth:100, align:"center"},
            {field: 'shop_wangwang', title: '店铺旺旺', align:'center'},
            {field: 'shop_type', title: '店铺类型', align:'center'},
            {field: 'shop_addTime', title: '添加时间', align:'center'},
            {field: 'shop_isCheck', title: '审核情况', align:'center',templet:function(d){
            	if (localStorage.login_role==2) {
            		if(d.shop_isCheck=="yes"){
            			return '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal">审核通过</button>';
            		}else{
            			return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="reason">审核未通过</button>';
            		}
            	} else{
            		if (d.shop_isCheck=="wait") {
            			return '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="goCheck">未审核</button>';
            		} else if(d.shop_isCheck=="yes"){
            			return '<button type="button" class="layui-btn layui-btn-sm layui-btn" lay-event="goCheck">已通过</button>';
            		} else{
            			return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="goCheck">未通过</button>';
            		}
            	}
            }},
            {title: '操作', minWidth:175, templet:'#shopListBar',fixed:"right",align:"center"}
        ]]
    });
	
	
    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
    	var type = $(this).data('type');
    	//console.log(type);
        if($(".searchVal").val() != ''){
            table.reload("shopListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'post',
                url:'http://127.0.0.1:88/index.php/shop/shop/shopSearch',
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    user_name:localStorage.login_name
                },
                contentType: 'application/json',
                done: function(res, curr, count){
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
              }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addShop(edit){
        var index = layui.layer.open({
            title : "添加商铺",
            type : 2,
            area:["450px","400px"],
            content : "shopAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".shop_name").val(edit.shop_name);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                /*if(edit){
                    body.find(".userName").val(edit.userName);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                }*/
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
    	if (localStorage.login_role==1) {
    		layer.msg("不能进行此操作");
    	} else{
    		addShop();
    	}
        
    })
	//刷新shop_refresh
	$(".shop_refresh").click(function(){
        tableIns.reload();
    })
	
    //批量删除
    $(".delAll_btn").click(function(){
        if (localStorage.login_role==1) {
        	layer.msg("不能进行此操作");
        } else{
        	var checkStatus = table.checkStatus('shopListTable'),
            data = checkStatus.data,
            shop_name = [];
            
	        if(data.length > 0) {
	            for (var i in data) {
	                shop_name.push(data[i].shop_name);
	            }
	            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
	                $.get("http://127.0.0.1:88/index.php/shop/shop/delMoreShop",{
	                    shop_name : shop_name  //将需要删除的shop_name作为参数传入
	                },function(data){
	                tableIns.reload();
	                layer.close(index);
	                })
	            })
	        }else{
	            layer.msg("请选择需要删除的用户");
	        }
        }
    })

    //列表操作
    table.on('tool(shopList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
        	//console.log(data.shop_id);
            if (localStorage.login_role==1) {
	        	layer.msg("不能进行此操作");
	        } else{
            	var index = layui.layer.open({
		            title : "编辑商铺",
		            type : 2,
		            area:["450px","400px"],
		            content : "shopEdit.html",
		            success : function(layero, index){
		                var body = layui.layer.getChildFrame('body', index);
		                body.find(".shop_name").val(data.shop_name);
		                body.find(".shop_wangwang").val(data.shop_wangwang);
		                body.find(".shop_img_url").val(data.shop_img_url);
		                body.find("#shop_id").val(data.shop_id);
		                /*$("input[name=shop_type][value='淘宝']").attr("checked", data.shop_type == "淘宝" ? true : false);
		               	$("input[name=shop_type][value='京东']").attr("checked", data.shop_type == "京东" ? true : false);
		               	$("input[name=shop_type][value='拼多多']").attr("checked", data.shop_type == "拼多多" ? true : false);
		               	$("input[name=shop_type][value='阿里巴巴']").attr("checked", data.shop_type == "阿里巴巴" ? true : false);*/
		               // body.find(".shop_type input[value=2]").attr("checked",true);
		                body.find(".shop_img").attr("src",data.shop_img_url);
		                form.render('radio');
		                /*if(edit){
		                    body.find(".userName").val(edit.userName);  //登录名
		                    body.find(".userEmail").val(edit.userEmail);  //邮箱
		                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
		                    body.find(".userGrade").val(edit.userGrade);  //会员等级
		                    body.find(".userStatus").val(edit.userStatus);    //用户状态
		                    body.find(".userDesc").text(edit.userDesc);    //用户简介
		                    form.render();
		                }*/
		                setTimeout(function(){
		                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
		                        tips: 3
		                    });
		                },500)
		            }
		        })
            }
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
        	if (localStorage.login_role==1) {
	        	layer.msg("不能进行此操作");
	        } else{
	        	layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
	                $.get("http://127.0.0.1:88/index.php/shop/shop/delShop",{
	                    id : data.shop_name  //将需要删除的newsId作为参数传入
	                },function(data){
	                	if(data.code==200){
	                		layer.msg(data.msg);
	                		setTimeout(function(){
	                			tableIns.reload();
	                    		layer.close(index);
	                		},1000);
	                	}else{
	                		layer.msg(data.msg);
	                	}
	                })
	            });
	        }
        }else if(layEvent === 'detail'){
        	var index = layui.layer.open({
	            title : "店铺详情",
	            type : 2,
	            area:["450px","420px"],
	            content : "shopDetail.html",
	            success : function(layero, index){
	            	var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/shop/shop/detailShop",{
	                    id : data.shop_name  //将需要删除的newsId作为参数传入
	                },function(data){
	                	
	                	body.find(".shop_name").val(data.detail[0].shop_name);
	                    body.find(".shop_wangwang").val(data.detail[0].shop_name);
	                    body.find(".shop_type").val(data.detail[0].shop_name);
	                    body.find(".shop_img").attr("src",data.detail[0].shop_img_url);
	                    form.render();
	                })
	                /*var body = layui.layer.getChildFrame('body', index);
	                
	                    body.find(".shop_name").val("detail.userName");  //登录名
	                    body.find(".userEmail").val(edit.userEmail);  //邮箱
	                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
	                    body.find(".userGrade").val(edit.userGrade);  //会员等级
	                    body.find(".userStatus").val(edit.userStatus);    //用户状态
	                    body.find(".userDesc").text(edit.userDesc);    //用户简介
	                    form.render();*/
	                
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
        	})
        }else if(layEvent === 'goCheck'){
        	var index = layui.layer.open({
	            title : "店铺审核",
	            type : 2,
	            area:["450px","330px"],
	            content : "shopCheck.html",
	            success : function(layero, index){
	            	/*var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/shop/shop/checkShop",{
	                    id : data.shop_name  //将需要删除的newsId作为参数传入
	                },function(data){
	                	
	                })*/
	                var body = layui.layer.getChildFrame('body', index);
		            body.find(".shop_name").val(data.shop_name);
		            body.find(".check_desc").text(data.check_desc);
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
        	})
        }else if(layEvent === 'reason'){
        	var index = layui.layer.open({
	            type: 1
	            ,title:'不通过原因'
		        ,content: '<div style="padding: 20px 100px;">'+data.check_desc+'</div>'
		        ,shade: 0 //不显示遮罩
		        ,success: function(){
		          /*setTimeout(function(){
	                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)*/
		        }
        	})
        }
    });

})